package com.xuxueli.commontdemo.commont;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.lang.reflect.Method;

/**
 * @CLassName OperateLogAspect
 * @Description 操作日志切面
 **/
@Aspect
@Component
@Slf4j
public class OperateLogAspect {

    @Pointcut("execution(public * com.xuxueli.commontdemo.controller.*.*(..))")
    public void pointcut() {
    }

    @Around(value = "pointcut()")
    public Object process(ProceedingJoinPoint joinPoint) throws Throwable {
        Method targetMethod = ((MethodSignature) joinPoint.getSignature()).getMethod();
        Method currentMethod = ClassUtils.getUserClass(joinPoint.getTarget().getClass())
                .getDeclaredMethod(targetMethod.getName(), targetMethod.getParameterTypes());
        InterfaceOperateLog operateLog = currentMethod.getAnnotation(InterfaceOperateLog.class);
        if(null!=operateLog){
            System.out.println(operateLog.type());
        }
        return joinPoint.proceed();
    }
}
